<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
            v-model="queryParams.orderNumber"
            clearable
            placeholder="请输入订单编号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['reductionrecords:reductionrecords:add']"
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['reductionrecords:reductionrecords:edit']"
            :disabled="single"
            icon="Edit"
            plain
            type="success"
            @click="handleUpdate"
        >编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['reductionrecords:reductionrecords:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['reductionrecords:reductionrecords:export']"
            icon="Download"
            plain
            type="warning"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reductionrecordsList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
      <el-table-column align="center" label="订单编号" prop="orderNumber" width="90"/>
      <el-table-column align="center" label="日龄(天)" prop="ageInDays" width="80"/>
      <el-table-column align="center" label="鸡舍" prop="coopId" width="120">
        <template #default="scope">
          <dict-tag :options="hen_location" :value="scope.row.coopId"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="鸡舍位置" prop="location" width="150"/>
      <el-table-column align="center" label="批次" prop="batchId" width="90"/>
      <el-table-column align="center" label="减少数量/只" prop="reductionCount" width="100"/>
      <el-table-column align="center" label="母鸡数量/只" prop="roosterCount" width="100"/>
      <el-table-column align="center" label="公鸡数量/只" prop="henCount" width="100"/>
      <el-table-column align="center" label="品种" prop="breed" width="80"/>
      <el-table-column align="center" label="重量/KG" prop="weight" width="80"/>
      <el-table-column align="center" label="原因" prop="reason" width="140"/>
      <el-table-column align="center" label="创建时间" prop="createdAt" width="100">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="135">
        <template #default="scope">
          <el-button v-hasPermi="['reductionrecords:reductionrecords:edit']" icon="Edit" link type="primary"
                     @click="handleUpdate(scope.row)">编辑
          </el-button>
          <el-button v-hasPermi="['reductionrecords:reductionrecords:remove']" icon="Delete" link type="primary"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改死淘明细对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="700px">
      <el-form ref="reductionrecordsRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="订单编号" prop="orderNumber">
              <el-input v-model="form.orderNumber" placeholder="请输入订单编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="日龄(天)" prop="ageInDays">
              <el-input v-model="form.ageInDays" placeholder="请输入日龄(天)"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="鸡舍" prop="coopId">
              <el-select v-model="form.coopId" placeholder="请选择鸡舍">
                <el-option
                    v-for="dict in hen_location"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="批次" prop="batchId">
              <el-input v-model="form.batchId" placeholder="请输入批次"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="减少数量" prop="reductionCount">
              <el-input v-model="form.reductionCount" placeholder="请输入减少数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="母鸡数量" prop="roosterCount">
              <el-input v-model="form.roosterCount" placeholder="请输入母鸡数量"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="公鸡数量" prop="henCount">
              <el-input v-model="form.henCount" placeholder="请输入公鸡数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="品种" prop="breed">
              <el-input v-model="form.breed" placeholder="请输入品种"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="重量" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入重量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="input-column">
            <el-form-item label="原因" prop="reason">
              <el-input v-model="form.reason" placeholder="请输入原因"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" class="input-column">
            <el-form-item label="创建时间" prop="createdAt">
              <el-date-picker v-model="form.createdAt"
                              clearable
                              placeholder="请选择创建时间"
                              type="date"
                              value-format="YYYY-MM-DD">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Reductionrecords" setup>
import {
  addReductionrecords,
  delReductionrecords,
  getReductionrecords,
  listReductionrecords,
  updateReductionrecords
} from "@/api/reductionrecords/reductionrecords";

const {proxy} = getCurrentInstance();
const {hen_location} = proxy.useDict('hen_location');

const reductionrecordsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNumber: null,
  },
  rules: {
    orderNumber: [
      {required: true, message: "订单编号不能为空", trigger: "blur"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询死淘明细列表 */
function getList() {
  loading.value = true;
  listReductionrecords(queryParams.value).then(response => {
    reductionrecordsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNumber: null,
    ageInDays: null,
    coopId: null,
    batchId: null,
    reductionCount: null,
    roosterCount: null,
    henCount: null,
    breed: null,
    weight: null,
    reason: null,
    createdAt: null
  };
  proxy.resetForm("reductionrecordsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加死淘明细";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getReductionrecords(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改死淘明细";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reductionrecordsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateReductionrecords(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReductionrecords(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除死淘明细编号为"' + _ids + '"的数据项？').then(function () {
    return delReductionrecords(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('reductionrecords/reductionrecords/export', {
    ...queryParams.value
  }, `reductionrecords_${new Date().getTime()}.xlsx`)
}

getList();
</script>
